<?php


abstract class WSB_View_Helper_FormSelectCsvAdv extends Zend_View_Helper_FormSelect
{

	protected $_elementId;
	protected $_helperName;
	protected $_helperId;
	protected $_helperValue;
	protected $_helperOptions;
	protected $_helperAttribs;

	public function formSelectCsvAdv($name, $value = null, $attribs = null, $listsep = "<br />\n")
	{
        $info = $this->_getInfo($name, $value, $attribs, null, $listsep);
        extract($info); // name, id, value, attribs, options, listsep, disable

		$this->_elementId = $this->_buildElementId($name);

      	// Helper Definitionen
      	$this->_helperName		= $this->_buildHelperName($name);
		$this->_helperId		= $this->_buildElementId($this->_helperName);
      	$this->_helperValue		= '';
      	$this->_helperOptions	= $this->_csvToArray($value);
		$this->_helperOptions	= $this->_testFileExistance($this->_helperOptions);

      	$this->_helperAttribs 			= $attribs;
		$this->_helperAttribs['style']	= 'width:100%;';
		$this->_helperAttribs['id']		= $this->_helperId;
		$this->_helperAttribs['size']	= empty($attribs['size']) ? 5 : $attribs['size'];
		$this->_helperAttribs['size']	= max($this->_helperAttribs['size'], count($this->_helperOptions));

      	$str = $this->formSelect($this->_helperName, $this->_helperValue, $this->_helperAttribs, $this->_helperOptions, $listsep);

		$str .= $this->_getAddLink();
		$str .= sprintf(' | <a href="javascript:deleteOption(\'%s\',\'%s\');">Delete</a>', $this->_helperId, $this->_elementId);
		if($this->_helperAttribs['size'] > 1) {
			$str .= sprintf(' | <a href="javascript:moveOptionUp(\'%s\',\'%s\');">Up</a>', $this->_helperId, $this->_elementId);
			$str .= sprintf(' | <a href="javascript:moveOptionDown(\'%s\',\'%s\');">Down</a>', $this->_helperId, $this->_elementId);
		}
		$str .= $this->_getAdditionalHtml();
      	$str .= $this->_buildJs();
		$str .= $this->_hidden($name, $value, array('id' => $this->_elementId));

		return $str;
	}

    abstract protected function _getAddLink();
	abstract protected function _getAdditionalHtml();

	protected function _testFileExistance(array $files)
	{
		foreach($files AS $key=>$value) {
			if(!is_file(WSB_PATH . $key)) {
				$files[$key] = $value . ' [!]';
			}
		}
		return $files;
	}

	protected function _buildHelperName($name)
	{
		$name = preg_replace('#[^A-Za-z0-9]#', '_', $name);
		$name = preg_replace('#_$#', '', $name);
		return '__helper_'.$name;
	}

	protected function _buildElementId($name)
	{
		$name = preg_replace('#[^A-Za-z0-9]#', '_', $name);
		$name = preg_replace('#_$#', '', $name);
		return $name;
	}

	protected function _csvToArray($csv)
	{
        $arr = array();
      	foreach(WSB_Div::csvToArray($csv, ',') AS $k=>$v) {
      		$arr[$v] = $v;
      	}
      	return $arr;
	}

    protected function _buildJS()
	{
    	static $count;
		$js = "<script type=\"text/javascript\">

			var formName = '';
			var postfix = '';

			function addOption(selectId, hiddenId, value, text) {
				var selectElement = document.getElementById(selectId);
				var hiddenElement = document.getElementById(hiddenId);
				newOption = new Option(text, value, false, true);
				var newIndex = selectElement.length;
				if(selectElement.size == 1) {
					if(newIndex == 1) {
						newIndex = 0;
					}
				}
				selectElement.options[newIndex] = newOption;
				setHiddenField(selectId, hiddenId);
			}

			function deleteOption(selectId, hiddenId) {
				var selectElement = document.getElementById(selectId);
				var selectedIndex = selectElement.selectedIndex;
				selectElement.options[selectedIndex]=null;
				setHiddenField(selectId, hiddenId);
			}

			function moveOptionUp(selectId, hiddenId) {
				var srcIndex = document.getElementById(selectId).selectedIndex;
				if(srcIndex > 0) {
					var dstIndex = srcIndex-1;
					switchOption(selectId, hiddenId, srcIndex, dstIndex);
					setHiddenField(selectId, hiddenId);
				}
			}

			function moveOptionDown(selectId, hiddenId) {
				var selectElement = document.getElementById(selectId);
				var srcIndex = selectElement.selectedIndex;
				var optionsCount = selectElement.length;
				if(srcIndex < (optionsCount-1)) {
					var dstIndex = srcIndex+1;
					switchOption(selectId, hiddenId, srcIndex, dstIndex);
					setHiddenField(selectId, hiddenId);
				}
			}

			function switchOption(selectId, hiddenId, srcIndex, dstIndex) {
				var selectElement = document.getElementById(selectId);
				srcOption = new Option(selectElement.options[srcIndex].text, selectElement.options[srcIndex].value, false, true);
				dstOption = new Option(selectElement.options[dstIndex].text, selectElement.options[dstIndex].value, false, true);
				selectElement.options[srcIndex] = dstOption;
				selectElement.options[dstIndex] = srcOption;
			}

			function setHiddenField(selectId, hiddenId) {
				var selectElement = document.getElementById(selectId);
				var hiddenElement = document.getElementById(hiddenId);
				var csvString = '';
				var optionsCount = selectElement.length;
				for (i = 0; i < optionsCount; ++i) {
					csvString += selectElement.options[i].value;
					if(i < (optionsCount-1)) {
						csvString += ',';
					}
				}
				hiddenElement.value = csvString;
			}

			</script>";
		if($count == 0) {
			$count++;
			return $js;
		}
		return '';
    }

}